<template>
  <div>
    <el-breadcrumb separator="/" style="padding-left:10px;padding-bottom:10px;font-size:12px;">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>房屋管理</el-breadcrumb-item>
      <el-breadcrumb-item>房屋审核</el-breadcrumb-item>
    </el-breadcrumb>
    <!--公告列表卡片-->
    <el-card class="box-card card" id="card">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item size="mini" label="状态" label-width="70px" style="font-weight: bold">
          <el-select clearable v-model="searchForm.state" placeholder="请选择" :style="{width: '100%'}">
            <el-option label="未审核" :value="0"></el-option>
            <el-option label="审核未通过" :value="1"></el-option>
            <el-option label="审核通过" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item size="mini" label="房屋类型" label-width="70px" style="font-weight: bold">
          <el-select clearable v-model="searchForm.houseType" placeholder="请选择" :style="{width: '100%'}">
            <el-option label="商品房" :value="0"></el-option>
            <el-option label="单身公寓住宅" :value="1"></el-option>
            <el-option label="单元式住宅" :value="2"></el-option>
            <el-option label="复式住宅" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item size="mini" label="租期" label-width="70px" style="font-weight: bold">
          <el-select clearable v-model="searchForm.leaseTerm" placeholder="请选择" :style="{width: '100%'}">
            <el-option label="三月" :value="90"></el-option>
            <el-option label="半年" :value="180"></el-option>
            <el-option label="一年" :value="360"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item size="mini" label="装修情况" label-width="70px" style="font-weight: bold">
          <el-select clearable v-model="searchForm.decorateState" placeholder="请选择" :style="{width: '100%'}">
            <el-option label="精美装修" :value="0"></el-option>
            <el-option label="简单装修" :value="1"></el-option>
            <el-option label="毛坯装修" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item size="mini" style="margin-left: 10px;">
          <el-button icon="el-icon-refresh" @click="resetForm">重置</el-button>
          <el-button icon="el-icon-search" @click="onSubmit(1)" type="primary">查询</el-button>
        </el-form-item>
      </el-form>
      <!--表格内容显示区域-->
      <el-table
        :max-height="this.getTableMaxHeight()"
        v-loading="loading" element-loading-text="数据加载中..."
        :data="page.records"
        border
        size="mini"
        style="width: 100%;">
        <el-table-column
          width="60px"
          align="center"
          type="index"
          label="序号">
        </el-table-column>
        <el-table-column
          width="200px"
          align="center"
          prop="address"
          label="所在地">
          <template slot-scope="scope">
            <span class="el-tag el-tag--primary el-tag--mini el-tag--light">{{ scope.row.address }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="150px"
          align="center"
          prop="addressInfo"
          label="详细地址">
        </el-table-column>
        <el-table-column
          width="80px"
          align="center"
          type="userName"
          label="房东名">
          <template slot-scope="scope">
            <span class="el-tag el-tag--primary el-tag--mini el-tag--light">{{ scope.row.userName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="120px"
          align="center"
          type="phoneNumber"
          label="联系电话">
          <template slot-scope="scope">
            <span class="el-tag el-tag--primary el-tag--mini el-tag--light">{{ scope.row.phoneNumber }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="80px"
          align="center"
          prop="state"
          label="状态">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.state === 0">已上架</el-tag>
            <el-tag type="success" v-else-if="scope.row.state === 1">已出租</el-tag>
            <el-tag type="info" v-else-if="scope.row.state === 2">已过期</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          width="110px"
          align="center"
          prop="verifyState"
          label="租赁状态">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.verifyState === 0">未审核</el-tag>
            <el-tag type="danger" v-else-if="scope.row.verifyState === 1">审核未通过</el-tag>
            <el-tag type="success" v-else-if="scope.row.verifyState === 2">审核通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          width="100px"
          align="center"
          prop="avatar"
          label="图片">
          <template slot-scope="scope">
            <el-popover placement="right" trigger="hover">

              <img :src="scope.row.avatar" style="height: 500px;width: 500px" alt="alt"/>
              <img slot="reference" :src="scope.row.avatar" alt="alt" style="height: 50px;width: 50px;cursor: pointer">
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column
          width="70px"
          align="center"
          prop="price"
          label="租金(元)">
        </el-table-column>
        <el-table-column
          width="120px"
          align="center"
          prop="houseType"
          label="房屋类型">
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.houseType === 0">商品房</el-tag>
            <el-tag type="warning" v-else-if="scope.row.houseType === 1">公寓式住宅</el-tag>
            <el-tag type="warning" v-else-if="scope.row.houseType === 2">单元式住宅</el-tag>
            <el-tag type="warning" v-else-if="scope.row.houseType === 3">复式住宅</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          width="70px"
          align="center"
          prop="area"
          label="面积㎡">
        </el-table-column>
        <el-table-column
          width="100px"
          align="center"
          prop="decorateState"
          label="装修情况">
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.decorateState === 0">精美装修</el-tag>
            <el-tag type="warning" v-else-if="scope.row.decorateState === 1">简单装修</el-tag>
            <el-tag type="warning" v-else-if="scope.row.decorateState === 2">毛坯装修</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          width="70px"
          align="center"
          prop="leaseTerm"
          label="租期">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.leaseTerm === 90">三月</el-tag>
            <el-tag v-else-if="scope.row.leaseTerm === 180">半年</el-tag>
            <el-tag v-else-if="scope.row.leaseTerm === 360">一年</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="审核通过" placement="top" :enterable="false">
              <el-button @click="statusSuccess(scope.row.id)" size="mini" type="success" icon="el-icon-check"
                         circle></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="审核不通过" placement="top" :enterable="false">
              <el-button @click="show(scope.row.id)" size="mini" type="warning" icon="el-icon-close"
                         circle></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        v-if="page.total > 0"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.current"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </el-card>
    <HouseErrorInfo :houseErrorInfo="houseErrorInfo"
                 :isEdit="isEdit"
                 :id="id"
                 @dataUpdate="init"
                 @changeShow="showAddOrUpdate">
    </HouseErrorInfo>
  </div>
</template>

<script>
  import { getHouseInfoListPage,updateStatusSuccess,updateStatusFail,searchHouseInfoListPage } from '@/api/houseInfo'
  import HouseErrorInfo from './HouseErrorInfo'
  export default {
    name: 'ExamineHouseInfo',
    data () {
      return {
        searchForm: {
          state: '',
          houseType: '',
          leaseTerm: '',
          decorateState: ''
        },
        page: {
          records: [],
          total: 0,
          size: 0,
          current: 0,
          orders: []
        },
        pageNum: 1,
        pageSize: this.getPageSize(),
        // 控制新增编辑弹窗的显示与隐藏
        houseErrorInfo: false,
        isEdit: false,
        id: 0,
        loading: false,

      }
    },
    components:{
      HouseErrorInfo
    },
    created () {
      this.init()
    },
    methods: {
      init () {
        const _this = this
        _this.loading = true
        /* 创建组件的时候加载页面信息 */
        getHouseInfoListPage(_this.pageNum, _this.pageSize).then(response => {
          _this.page = response.data
        }).finally(() => {
          _this.loading = false
        })
      },
      handleSizeChange (val) {
        /* 将value赋值给size, 重新查询数据 */
        this.pageSize = val
        this.onSubmit()
      },
      handleCurrentChange (val) {
        this.pageNum = val
        this.onSubmit()
      },
      resetForm () {
        this.searchForm.houseType = ''
        this.searchForm.decorateState = ''
        this.searchForm.leaseTerm = ''
        this.searchForm.verifyState = ''
        this.pageNum = 1
        this.pageSize = this.getPageSize()
        const _this = this
        getHouseInfoListPage(this.pageNum, this.pageSize).then(response => {
          _this.page = response.data
        })
      },
      onSubmit (page = this.pageNum) {
        const _this = this
        searchHouseInfoListPage(this.searchForm, page, this.pageSize).then(response => {
          _this.page = response.data
        })
      },
      statusSuccess (id) {
        this.$confirm('是否审核通过该房屋信息?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const _this = this
          updateStatusSuccess(id).then(response => {
            _this.init()
            _this.RightMessage(_this, '成功', response.message, 'success')
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消审核房屋'
          })
        })
      },
      statusFail (id) {
        this.$confirm('是否设置审核不通过该房屋信息?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const _this = this
          updateStatusFail(id).then(response => {
            _this.init()
            _this.RightMessage(_this, '成功', response.message, 'success')
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消审核房屋'
          })
        })
      },
      // 按钮点击事件 显示新增编辑弹窗组件
      show (id) {
        this.houseErrorInfo = true
          this.id = id
      },
      // 监听 子组件弹窗关闭后触发，有子组件调用
      showAddOrUpdate (data) {
        this.houseErrorInfo = data !== 'false'
      },
    },
    mounted () {
      // 获取屏幕高度
      document.getElementById('card').style.height = this.getWindowHeight()
    }
  }
</script>

<style scoped>

</style>
